package com.example.yin.controller;


import com.example.yin.common.ErrorMessage;
import com.example.yin.common.SuccessMessage;
import com.example.yin.domain.Forum;
import com.example.yin.vo.ForumVO;
import com.example.yin.service.ForumService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 *
 * @author
 * @since 2023-06-21
 */
@RestController
public class ForumController {
    @Autowired
    private ForumService forumService;


    @RequestMapping(value = "/forum/delete", method = RequestMethod.GET)
    public Object deleteForum(HttpServletRequest req) {
        String id = req.getParameter("id");

        boolean res = forumService.deleteForum(Integer.parseInt(id));
        if (res) {
            return new SuccessMessage<ObjectUtils.Null>("删除成功").getMessage();
        } else {
            return new ErrorMessage("删除失败").getMessage();
        }
    }



    @ResponseBody
    @RequestMapping(value = "/forum/add", method = RequestMethod.POST)
    public Object addForum(HttpServletRequest req) {
        Integer user_id = Integer.valueOf(req.getParameter("user_id").trim());
        String content = req.getParameter("content").trim();
//        String create_time = req.getParameter("create_time").trim();

        Forum forum = new Forum();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date createtime = new Date();
//        try {
//            createtime = dateFormat.parse(create_time);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
        forum.setUserId(user_id);
        forum.setContent(content);
//        forum.setCreate_time(createtime);

        boolean res = forumService.addForum(forum);
        if (res) {
            return new SuccessMessage<ObjectUtils.Null>("添加成功").getMessage();
        } else {
            return new ErrorMessage("添加失败").getMessage();
        }
    }



    @ResponseBody
    @RequestMapping(value = "/forum/update", method = RequestMethod.POST)
        public Object updateForum(HttpServletRequest req) {
            String id = req.getParameter("id").trim();
            Integer user_id = Integer.valueOf(req.getParameter("user_id").trim());
            String content = req.getParameter("content").trim();
            String create_time = req.getParameter("create_time").trim();

            Forum forum = new Forum();
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date createtime = new Date();
            try {
                createtime = dateFormat.parse(create_time);
            } catch (Exception e) {
                e.printStackTrace();
            }
            forum.setUserId(user_id);
            forum.setContent(content);
            forum.setCreate_time(createtime);
            forum.setId(Integer.valueOf(id));

            boolean res = forumService.updateForum(forum);
            if (res) {
                return new SuccessMessage<ObjectUtils.Null>("修改成功").getMessage();
            } else {
                return new ErrorMessage("修改失败").getMessage();
            }
    }



    @RequestMapping(value = "/forum", method = RequestMethod.GET)
    public Object allForum() {
        return new SuccessMessage<List<ForumVO>>(null, forumService.allForum()).getMessage();
    }
}
